#include "ball.h"

#include "shapes3d.h"
#include <gl\glew.h>
#include <gl\glfw.h>
#include "Shader.h"

#include "box.h"

#define STRINGIFY(A)  #A


using namespace TorPhysix;


TorPhysix::Ball::Ball() : TorPhysix::PhysicsObject()
{
	collisionSphere = new CollisionSphere;
	collisionPrimitive = collisionSphere;

	normalShader = new Shader();
	char vert[]= "varying vec3 normal;"
		"void main(){"
		"gl_Position    = gl_ModelViewProjectionMatrix * gl_Vertex;"
		"gl_FrontColor  = gl_Color;"
		"normal = gl_Normal;"
		"gl_TexCoord[0] = gl_MultiTexCoord0;"
		"}";

	char frag[]=
		"varying vec3 normal;"
		"void main()"
		"{"
		"	gl_FragColor = vec4( normal * 0.5 + 0.5, 1.0  );"
		"}";

	if( !normalShader->installFromCString( vert, frag ) )
	{
		int shaderfail = 1;
	}

	normalShader->end(); // dont want it to default

}

void Ball::vRender()
{
	normalShader->begin();
	glPushMatrix();
	{
		Mat4f transform =collisionPrimitive->body->getTransform();
		transform = transpose( transform ); // GL matrices are transposed to what we are used to in math.
		glMultMatrixf( &transform[0] );
		Shapes3D::DrawSphere(collisionSphere->radius, 12, 12);
	}glPopMatrix();
	normalShader->end();
}

void Ball::vPicking()
{
	glPushMatrix();
	{
		Mat4f transform = collisionPrimitive->body->getTransform();
		transform = transpose( transform ); // GL matrices are transposed to what we are used to in math.
		glMultMatrixf( &transform[0] );
		glColor3f(m_colorID[0]/255.0f, m_colorID[1]/255.0f, m_colorID[2]/255.0f);
		Shapes3D::DrawSphere(collisionSphere->radius, 12, 12);
	}glPopMatrix();
}

void Ball::vRenderShadow()
{

	glPushMatrix();
	{
		glTranslatef(0.0f, 0.02f, 0.0f);
		glScalef(1.0f, 0, 1.0f);
		Mat4f transform = collisionPrimitive->body->getTransform();
		transform = transpose( transform ); // GL matrices are transposed to what we are used to in math.
		glMultMatrixf(&transform[0]);
		Shapes3D::DrawSphere(collisionSphere->radius, 8, 8);
	}
	glPopMatrix();
	
}

void Ball::vRenderDebug()
{
	glLineWidth(1.5f);
	glBegin(GL_LINES);
	glColor3f(1,1,1);
	Vec3f pos = collisionPrimitive->body->getPosition();
	glVertex3f(pos.x(), pos.y(), pos.z() );
	pos += collisionPrimitive->body->getVelocity() * 0.2f;
	glVertex3f( pos.x(), pos.y(), pos.z() );

	glColor3f(1,0,1);
	glVertex3f( collisionPrimitive->body->getPosition().x(),  collisionPrimitive->body->getPosition().y(),  collisionPrimitive->body->getPosition().z() );
	Vec3f angularVelDir = collisionPrimitive->body->getPosition() + collisionPrimitive->body->getRotation();
	glVertex3f( angularVelDir.x(), angularVelDir.y(), angularVelDir.z() );
	glEnd();
	glLineWidth(1.0f);
}

void Ball::vScale( Vec3f scaleAxis, float scaleMagnitude )
{
	Vec3f scaleDelta = Vec3f(scaleAxis.z(), scaleAxis.y(), scaleAxis.x()) * scaleMagnitude;
	collisionSphere->scale(scaleDelta);
}

			



void TorPhysix::Ball::collideAgainst( TorPhysix::Box& box, CollisionData* cData )
{
	if( cData->hasMoreContacts() )
		TorPhysix::CollisionDetector::boxAndSphere( *(box.collisionBox), *(this->collisionSphere) , cData );
}

void TorPhysix::Ball::collideAgainst( Ball& ball, CollisionData* cData )
{
	if (!cData->hasMoreContacts()) return;
		TorPhysix::CollisionDetector::sphereAndSphere( *(ball.collisionSphere), *(this->collisionSphere), cData );
}
